package com.xish.api.trade;

import com.xish.exception.XishException;
import com.xish.model.bean.WzmActivityTrade;
import com.xish.util.HttpClientUtil;
import com.xish.util.SignUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.TreeMap;

@Service
public class TradeService {

    @Value("#{envFactory.get('trade_api') }")
    private String trade_api;
    @Value("#{envFactory.get('trade_app_key') }")
    private String trade_app_key;
    @Value("#{envFactory.get('trade_app_secret') }")
    private String trade_app_secret;


    @Autowired
    private TradeRepository tradeRepository;


    public WzmActivityTrade findByActivityId(int activityId) {
        return tradeRepository.findByActivityId(activityId);
    }

    //未完成
    public void tradeActivity(TreeMap<String,String> treeMap){
        //TreeMap<String,String> treeMap = new TreeMap<String,String>();
        treeMap.put("app_key",trade_app_key);
//        treeMap.put("id","26056");
//        treeMap.put("user_id","1");
//        treeMap.put("begin_date","2015-07-27");
//        treeMap.put("end_date","2015-08-27");
//        treeMap.put("type", "datelist");
        String sign = SignUtil.getSign(treeMap,trade_app_secret);
        treeMap.put("sign", sign);
        String resp = null;
        try {
            resp = HttpClientUtil.post(trade_api + "activity", treeMap);
        } catch (XishException e) {
            //todo
            e.printStackTrace();
        }
        System.out.println(resp);
    }
}
